package com.itqf.service.impl;

import com.itqf.exception.UserException;
import com.itqf.mapper.PermissionMapper;
import com.itqf.mapper.RoleMapper;
import com.itqf.mapper.UserMapper;
import com.itqf.pojo.Permission;
import com.itqf.pojo.Role;
import com.itqf.pojo.User;
import com.itqf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * projectName: rbacgp8
 *
 * @author: 赵伟风
 * time: 2021/8/12 11:25
 * description:
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;


    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public User authentication(String username){

       User user=  userMapper.queryByUsername(username);

       return user;
    }




    /**
     * 登录业务
     * 注意需要返回用户和返回用户的权限!
     * 如果用户账号或者密码错误,我们异常处理!
     *
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> login(User user) throws UserException {


        List<Permission> permissions = permissionMapper.queryByuId(user.getuId());


        List<Role> roles = roleMapper.queryHasAll(user.getuId());

        Map<String,Object> data = new HashMap<>();

        data.put("user", user);
        data.put("permissions", permissions);
        data.put("hasRoles", roles);

        System.out.println("data = " + data);

        return data;
    }
}
